<template>
	<view>
		<z-paging ref="paging" v-model="dataList" @query="queryList">
			<template #top>
				<view class="status_bar"></view>
			</template>

			<view class="item" v-for="(item, index) in dataList" :key="index">
				<view class="item-left">
					<image :src="item.headPicUrl"></image>
				</view>
				<view class="item-right">
					<view class="top">
						<view class="nick">{{item.nick}}</view>
						<image :src="getImageUrl('ic_sex_girl.png')" style="height: 25rpx;" mode="heightFix"
							v-if="2==item.sex"></image>
						<image :src="getImageUrl('ic_sex_boy.png')" style="height: 25rpx;" mode="heightFix"
							v-if="1==item.sex"></image>
						<view style="flex:1;"></view>
						<view class="time">{{stringToText(item.createTime)}}</view>
						<image class="delete" :src="getImageUrl('ic_delete.png')" @click="onDelete(item, index)"
							v-if="item.ownFlag"></image>
					</view>
					<view class="content">{{item.content}}</view>
					<view class="bottom" v-if="item.fileUrl">
						<view v-if="item.fileUrl.length==1">
							<image class="image-one" :src="item.fileUrl[0]" @click="onPreviewImage(item.fileUrl, 0)">
							</image>
						</view>
						<view v-if="item.fileUrl.length==2">
							<image class="image-two-left" :src="item.fileUrl[0]"
								@click="onPreviewImage(item.fileUrl, 0)"></image>
							<image class="image-two-right" :src="item.fileUrl[1]"
								@click="onPreviewImage(item.fileUrl, 1)"></image>
						</view>
						<view v-if="item.fileUrl.length==3"
							style="display: flex; flex-direction: row; align-items: center;">
							<image class="image-three-left" :src="item.fileUrl[0]"
								@click="onPreviewImage(item.fileUrl, 0)"></image>
							<view class="image-three-right">
								<image class="image-three-top" :src="item.fileUrl[1]"
									@click="onPreviewImage(item.fileUrl, 1)"></image>
								<image class="image-three-bottom" :src="item.fileUrl[2]"
									@click="onPreviewImage(item.fileUrl, 2)"></image>
							</view>
						</view>
						<view v-if="item.fileUrl.length>=4" style="display: flex; flex-direction: column; align-items: center;">
							<view class="image-four">
								
								<image class="image-four-small top-left-radius" :src="item.fileUrl[0]" @click="onPreviewImage(item.fileUrl, 0)"></image>
								<image class="image-four-big top-right-radius" :src="item.fileUrl[1]" mode="aspectFill" @click="onPreviewImage(item.fileUrl, 1)"></image>
							</view>
							<view class="image-four">
								<image class="image-four-big bottom-left-radius" :src="item.fileUrl[2]" mode="aspectFill"  @click="onPreviewImage(item.fileUrl, 2)"></image>
								<image class="image-four-small bottom-right-radius" :src="item.fileUrl[3]" @click="onPreviewImage(item.fileUrl, 3)"></image>
								<view class="image-other" v-if="item.fileUrl.length>4">+{{item.fileUrl.length - 4}}张 </view>
							</view>
						</view>
					</view>
				</view>
			</view>

			<template #bottom>
				<image :src="getImageUrl('ic_release.png')" class="release" @click="toRelease"></image>
			</template>
		</z-paging>

	</view>

</template>

<script>
	import TimeUtil from '@/main/common/TimeUtil.js'
	import RouteUtil from '@/main/common/RouteUtil';
	export default {
		data() {
			return {
				dataList: [],
			};
		},
		onLoad() {
			uni.$on('sendDynamicSuccess', () => {
				this.queryList(1, 20)
			})
		},
		onUnload() {
			uni.$off('sendDynamicSuccess')
		},
		methods: {
			queryList(pageNo, pageSize) {
				const params = {
					pageNo: pageNo,
					type: '1'
				}
				uni.$http.get("/api/dynamic/getDynamicList", params)
					.then(res => {
						this.$refs.paging.complete(res.data);
					}).catch(res => {
						this.$refs.paging.complete(false);
					})
			},
			onPreviewImage(list, index) {
				uni.previewImage({
					urls: list,
					current: index,
					fail: (error) => {
						console.log("error: " + JSON.stringify(error));
					}
				})
			},
			toRelease() {
				RouteUtil.toDynamicRelease()
			},
			onDelete(item, index) {
				uni.showModal({
					content: '确定要删除吗？',
					success: (res) => {
						if (res.confirm) {
							console.log("onDelete")
							this.requestDelete(item, index);
						}
					}
				})
			},
			requestDelete(item, index) {
				uni.$http.get('/api/dynamic/delOwnDynamic', {
						dynamicId: item.id
					})
					.then(res => {
						uni.$toasts.showToast('删除成功')
						this.dataList.splice(index, 1)
					})
			}, 
			stringToText(str){
				return TimeUtil.stringToText(str)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}

	.release {
		position: absolute;
		bottom: 30rpx;
		right: 30rpx;
		width: 100rpx;
		height: 100rpx;
		z-index: 99;
	}

	.item {
		display: flex;
		flex-direction: row;
		background-color: white;
		margin-bottom: 1px;
		padding: 30rpx;

		.item-left {
			margin-right: 20rpx;
			image {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
			}
		}

		.item-right {
			.top {
				display: flex;
				flex-direction: row;
				align-items: center;
				.nick {
					font-size: 30rpx;
					color: #222;
					font-weight: 500;
				}
				.time {
					font-size: 20rpx;
					color: #555;
				}
				.delete {
					width: 30rpx;
					height: 30rpx;
					margin-left: 15rpx;
				}
			}

			.content {
				margin-top: 15rpx;
				font-size: 28rpx;
				color: #555;
			}

			.bottom {
				display: flex;
				flex-direction: row;
				align-items: center;
				width: 590rpx;
				margin-top: 15rpx;
				.image-one {
					width: 350rpx;
					height: auto;
					aspect-ratio: 3 / 4;
					border-radius: 15rpx;
				}

				.image-two-left {
					width: 295rpx;
					aspect-ratio: 1;
					height: auto;
					border-top-left-radius: 15rpx;
					border-bottom-left-radius: 15rpx;
					// margin-right: 10rpx;
				}

				.image-two-right {
					width: 295rpx;
					height: auto;
					aspect-ratio: 1;
					border-top-right-radius: 15rpx;
					border-bottom-right-radius: 15rpx;
				}

				.image-three-left {
					width: 390rpx;
					aspect-ratio: 1;
					height: auto;
					border-top-left-radius: 15rpx;
					border-bottom-left-radius: 15rpx;
					// margin-right: 10rpx;
				}

				.image-three-right {
					position: relative;
					display: flex;
					flex-direction: column;
					align-items: center;

					.image-three-top {
						width: 195rpx;
						aspect-ratio: 1;
						height: auto;
						border-top-right-radius: 15rpx;
					}

					.image-three-bottom {
						width: 195rpx;
						aspect-ratio: 1;
						height: auto;
						border-bottom-right-radius: 15rpx;
					}

				}
				
				.image-four {
					position: relative;
					display: flex;
					flex-direction: row;
					align-items: center;
				
					.image-four-small {
						width: 195rpx;
						aspect-ratio: 1;
						height: auto;
					}
				
					.image-four-big {
						width: 390rpx;
						aspect-ratio: 2 / 1;
						height: auto;
					}
					
					.top-left-radius{
						border-top-left-radius: 15rpx;
					}
					.top-right-radius{
						border-top-right-radius: 15rpx;
					}
					.bottom-left-radius{
						border-bottom-left-radius: 15rpx;
					}
					.bottom-right-radius{
						border-bottom-right-radius: 15rpx;
					}
				
					.image-other {
						position: absolute;
						bottom: 10rpx;
						right: 10rpx;
						background-color: #222;
						border-radius: 5rpx;
						padding: 8rpx 12rpx;
						font-size: 18rpx;
						color: #fff;
					}
				}

			}

		}

	}
</style>